﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Undsf.RmmvDbms;
using Undsf.RmmvDbms.Attribute;
using Undsf.RmmvDbms.Data;
using HorizontalAlignment = System.Windows.Forms.HorizontalAlignment;

namespace RPGMaker_MV_Database_Manager
{
    public partial class FormMain : Form
    {
        // string CurrentProjectDir { get; set; }
        RmmvProject Project { get; set; }

        public FormMain()
        {
            InitializeComponent();
        }

        private void ToolStripMenuItemExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void ToolStripMenuItemFile_OpenProject_Click(object sender, EventArgs e)
        {
            OpenProject();
        }
        
        private void toolStripButtonOpen_Click(object sender, EventArgs e)
        {
            OpenProject();
        }

        private void OpenProject()
        {
            string projDir = null;
#if DEBUG
            folderBrowserDialog.SelectedPath = @"C:\Users\Arathi\Documents\Games\ProjectTemplate";
#endif
            folderBrowserDialog.Description = "请选择RMMV工程目录";
            if (folderBrowserDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            projDir = folderBrowserDialog.SelectedPath;
            Project = new RmmvProject();
            int result = Project.Load(projDir);
            if (result != RmmvProject.SUCCESS)
            {
                MessageBox.Show("打开工程失败！");
                return;
            }
            BindTreeView();
        }

        private void BindTreeView()
        {
            TreeNode standardActorNodes = new TreeNode();
            foreach (Actor actor in Project.Actors.Values)
            {
                if (actor == null) continue;
                TreeNode actorNode = new TreeNode(actor.ToString());
                actorNode.Tag = actor;
                standardActorNodes.Nodes.Add(actorNode);
            }
            standardActorNodes.Text = string.Format("标准单位 ({0})", standardActorNodes.Nodes.Count);
            treeView.Nodes.Add(standardActorNodes);
        }
        
        private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeView tree = (TreeView) sender;
            TreeNode selectedNode = tree.SelectedNode;
            Actor actor = (Actor) selectedNode.Tag;

            if (actor == null) return;

            listView1.Tag = actor;
            if (listView1.Columns.Count == 0)
            {
                listView1.Columns.Add("键", 100, HorizontalAlignment.Left);
                listView1.Columns.Add("值", listView1.Width-100, HorizontalAlignment.Left);
            }
            
            listView1.Items.Clear();

            Type type = actor.GetType();
            MemberInfo[] members = type.GetMembers();
            PropertyInfo[] properties = type.GetProperties();

            foreach (var prop in properties)
            {
                EntityField field = (EntityField)prop.GetCustomAttribute(typeof(EntityField));

                var item = new ListViewItem(field.Name);
                item.SubItems.Add(prop.GetValue(actor).ToString());

                listView1.Items.Add(item);
            }
            
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            ListView listView = (ListView) sender;
            ListViewItem item = listView.SelectedItems[0];
            Actor actor = (Actor) item.Tag;
            MessageBox.Show(item.SubItems[0].Text + ": " + item.SubItems[1].Text);
        }

        private void ToolStripMenuItemModule_ImportFromExcel_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            DQ1DataConverter importer = new DQ1DataConverter();
            importer.Convert(openFileDialog1.FileName);
            int maxId = 0;
            foreach (var obj in importer.Enemies.Values)
            {
                if (obj.id > maxId)
                {
                    maxId = obj.id;
                }
            }
            Enemy[] enemyList = new Enemy[maxId + 1];
            foreach (var obj in importer.Enemies.Values)
            {
                enemyList[obj.id] = obj;
            }
            string json = JsonConvert.SerializeObject(enemyList);
            File.WriteAllText("d:\\tmp\\enemies.json", json);
        }
    }
}
